package com.kaifamiao.base.Demo20210923;

import java.io.File;

/**
 * @author lucien-ma
 * @date 2021/9/23
 * @apiNote
 */
public class Demo02 {

    public static void main(String[] args) {
        File file = new File("E:/ycdl");
        long size = calculateSize(file);
        System.out.println(size);
    }

    /**
     * 计算指定文件的实际大小
     * @param file
     * @return
     */
    public static long calculateSize(File file) {
        long sum = 0;
        // 判断是否是文件夹
        if (file.isDirectory()) {
            // 获取该目录下所有文件
            File[] files = file.listFiles();
            for (File f : files) {
                sum += calculateSize(f);
            }
        } else if (file.isFile()) {
            sum += file.length();
        }
        return sum;
    }
}
